﻿using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RoundPanel
{
    [Designer("System.Web.UI.Design.WebControls.ContentDesigner, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"), ToolboxItem(false)]
    [ToolboxData("<{0}:RoundPanel runat=server></{0}:RoundPanel>")]
    public class RoundPanel : WebControl, INamingContainer
    {
        public string Title { get; set; }

        [Browsable(false), Description("The title template"), PersistenceMode(PersistenceMode.InnerProperty), TemplateInstance(TemplateInstance.Single)]
        public ITemplate TitleTemplate { get; set; }

        [Browsable(false), Description("The content template"), PersistenceMode(PersistenceMode.InnerDefaultProperty),
         TemplateInstance(TemplateInstance.Single)]
        public ITemplate ContentTemplate { get; set; }

        [Browsable(false), Description("The footer template"), PersistenceMode(PersistenceMode.InnerProperty), TemplateInstance(TemplateInstance.Single)]
        public ITemplate FooterTemplate { get; set; }

        public RoundPanel()
            : base(HtmlTextWriterTag.Div)
        {
        }

        protected override void OnInit(System.EventArgs e)
        {
            base.OnInit(e);
            Controls.Clear();
            if (!this.CssClass.Contains("round-panel"))
            {
                this.CssClass += " round-panel";
                this.CssClass = this.CssClass.Trim();
            }
            Controls.Add(CreateTitleArea());
            Controls.Add(CreateContentArea());
            Controls.Add(CreateFooterArea());
        }

        public override ControlCollection Controls
        {
            get
            {
                EnsureChildControls();
                return base.Controls;
            }
        }

        #region private methods

        private static WebControl CreateDiv(string css)
        {
            return new WebControl(HtmlTextWriterTag.Div) { CssClass = css };
        }

        private WebControl CreateTitleArea()
        {
            var topWrap = CreateDiv("rp-top");
            topWrap.Controls.Add(CreateDiv("rp-top-l"));
            topWrap.Controls.Add(CreateDiv("rp-top-r"));
            var topContent = CreateDiv("rp-top-m");
            if (this.TitleTemplate == null)
            {
                topContent.SetRenderMethodDelegate((w, c) => w.Write(this.Title));
            }
            else
            {
                this.TitleTemplate.InstantiateIn(topContent);
            }
            topWrap.Controls.Add(topContent);
            topWrap.Controls.Add(CreateDiv("clear"));
            return topWrap;
        }

        private WebControl CreateContentArea()
        {
            var outer = CreateDiv("rp-content");
            var inner = CreateDiv("rp-content-inner");
            if (this.ContentTemplate != null)
            {
                this.ContentTemplate.InstantiateIn(inner);
            }
            outer.Controls.Add(inner);
            return outer;
        }

        private WebControl CreateFooterArea()
        {
            var bottomWrap = CreateDiv("rp-bottom");
            bottomWrap.Controls.Add(CreateDiv("rp-bottom-l"));
            bottomWrap.Controls.Add(CreateDiv("rp-bottom-r"));
            var bottomContent = CreateDiv("rp-bottom-m");
            if (this.FooterTemplate != null)
            {
                this.FooterTemplate.InstantiateIn(bottomContent);
            }
            bottomWrap.Controls.Add(bottomContent);
            bottomWrap.Controls.Add(CreateDiv("clear"));
            return bottomWrap;
        }

        #endregion
    }
}